package github.leavesczy.reactivehttpsamples.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import github.leavesczy.reactivehttpsamples.R;

public class BridgeSpinnerItem extends LinearLayout {
    private Spinner spinner;
    private ArrayAdapter<SpinnerData> adapter;
    private SelectCallback mCallback;
    List<SpinnerData> items = new ArrayList<>();

    private TextView buttonTextView;
    private OnClickListener btnClickListener;

    public BridgeSpinnerItem(Context context) {
        this(context, null);
    }

    public BridgeSpinnerItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BridgeSpinnerItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        try (TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BridgeSpinnerItem)) {
            String leftText = ta.getString(R.styleable.BridgeSpinnerItem_bsLeftText);
            int leftTextColor = ta.getColor(R.styleable.BridgeSpinnerItem_bsLeftTextColor, Color.BLACK);
            float leftTextSize = ta.getDimension(R.styleable.BridgeSpinnerItem_bsLeftTextSize, 14);


            String leftBtnText=ta.getString(R.styleable.BridgeSpinnerItem_bsButtonText);
            int leftBtnTextColor = ta.getColor(R.styleable.BridgeSpinnerItem_bsButtonTextColor, Color.BLACK);
            float leftBtnTextSize = ta.getDimension(R.styleable.BridgeSpinnerItem_bsButtonTextSize, 12);

            setOrientation(LinearLayout.HORIZONTAL);
            setGravity(Gravity.CENTER_VERTICAL);
            setWeightSum(5);
            setBackgroundResource(R.drawable.shape_bridge_edit_item_bg);


            //添加左侧 Text 容器
            LinearLayout leftContain=new LinearLayout(context);
            leftContain.setOrientation(LinearLayout.HORIZONTAL);
            LayoutParams leftContainParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
            leftContainParams.weight = 2;
            leftContainParams.gravity=Gravity.CENTER_VERTICAL;
            leftContainParams.leftMargin = SizeUtils.dp2px(10);
            //text
            TextView leftTextView = new TextView(context);
            leftTextView.setText(leftText);
            leftTextView.setTextColor(leftTextColor);
            leftTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSize);
            LayoutParams leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            leftParams.rightMargin = SizeUtils.dp2px(5);
            leftParams.gravity=Gravity.CENTER_VERTICAL;
            leftTextView.setGravity(Gravity.CENTER_VERTICAL);
            leftContain.addView(leftTextView,leftParams);
            //判断是否展示按钮
            if(!StringUtils.isEmpty(leftBtnText)){
                buttonTextView=new TextView(context);
                buttonTextView.setTextColor(leftBtnTextColor);
                buttonTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,leftBtnTextSize);
                buttonTextView.setText(leftBtnText);
                buttonTextView.setPadding(SizeUtils.dp2px(5),SizeUtils.dp2px(1),SizeUtils.dp2px(5),SizeUtils.dp2px(1));
                buttonTextView.setBackgroundResource(R.drawable.shape_edit_button_btn);
                LinearLayout.LayoutParams rightBtParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                rightBtParams.gravity=Gravity.CENTER_VERTICAL;
                leftContain.addView(buttonTextView,rightBtParams);
                buttonTextView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(btnClickListener!=null){
                            btnClickListener.onClick(v);
                        }
                    }
                });
            }
            addView(leftContain, leftContainParams);



            //添加右侧的 Spinner
            spinner = new Spinner(context);
            LayoutParams rightParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
            rightParams.weight = 3;
            rightParams.rightMargin = SizeUtils.dp2px(10);
            addView(spinner, rightParams);



            adapter = new ArrayAdapter<SpinnerData>(context, R.layout.adapter_spinner_01, R.id.tvSpinnerKey, items);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(onItemSelectedListener);

        }
    }

    private AdapterView.OnItemSelectedListener onItemSelectedListener=new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if(Objects.nonNull(mCallback)){
                mCallback.onSelect(adapter.getItem(position));
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

    public void setItems(List<SpinnerData> items) {
        spinner.setOnItemSelectedListener(null);
        adapter.clear();
        if (CollectionUtils.isNotEmpty(items)) {
            adapter.addAll(items);
        }
        adapter.notifyDataSetChanged();

        spinner.setOnItemSelectedListener(onItemSelectedListener);
    }

    public SpinnerData getSelect(){
        Object selectedItem = spinner.getSelectedItem();
        if(Objects.isNull(selectedItem)){
            return null;
        }
        return (SpinnerData) selectedItem;
    }

    public void setSelectValue(String value){
        if(items.isEmpty()){
            return;
        }
        int selectIndex=0;
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).getValue().equals(value)) {
                selectIndex = i;
                break;
            }
        }
        spinner.setSelection(selectIndex);
    }

    public void setSelect(SpinnerData data){

    }

    public void setSelectCallback(SelectCallback callback){
            this.mCallback=callback;
    }

    public void setSpinnerEnable(boolean editable) {
        spinner.setEnabled(editable);
        if(buttonTextView!=null){
            buttonTextView.setEnabled(editable);
        }
    }

    public boolean getSpinnerEnable(){
       return spinner.isEnabled();
    }

    public interface SelectCallback{
        void onSelect(SpinnerData data);
    }


    public static class SpinnerData {
        private String name;
        private String value;

        public SpinnerData(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }


        @NonNull
        @Override
        public String toString() {
            return getName();
        }
    }


    public void initButtonTextListener(View.OnClickListener l){
        this.btnClickListener=l;
    }


}
